//
//  MEFileManager.swift
//  Test01
//
//  Created by 朱撷潼 on 15/3/14.
//  Copyright (c) 2015年 zhuxietong. All rights reserved.
//

import Foundation
import Eelay

extension FileManager
{
    @objc public static func removeTmp(filePrefix:String)
    {
        
        //        var path:Array = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,FileManager.SearchPathDomainMask.userDomainMask,true)
        //        let d_path = (path[0] as String) + "/tmp"
        
        let temp = NSTemporaryDirectory()
        
        let fileManager = FileManager.default
        guard let fileArray = fileManager.subpaths(atPath: temp) else {return}
        
        
        for fn in fileArray{
            
            //            print("KKKOOO|\(fn)")
            if fn.hasPrefix(filePrefix)
            {
                //                print("==========KK|\(filePrefix)")
                try! fileManager.removeItem(atPath: temp + "/\(fn)")
            }
        }
        
    }
}

extension FileManager{
    public static func getSize(url: URL)->UInt64?
    {
        var fileSize : UInt64 = 0
         
        do {
            let attr = try FileManager.default.attributesOfItem(atPath: url.path)
            fileSize = attr[FileAttributeKey.size] as! UInt64
             
            let dict = attr as NSDictionary
            fileSize = dict.fileSize()
        } catch {
            print("Error: \(error)")
            return nil
        }
         
        return fileSize
    }
}


class MEFileManager {
        
    public var fileManager:FileManager
        {
        get{
        return FileManager.default
        }
    }
    
    
    public func rename(_ newName:String,oldName:String,path:String) -> Bool
    {
        
        var ok = false
        let oldPath = "\(path)/\(oldName)"
        if self.fileManager.fileExists(atPath: oldPath)
        {
            let newPath = "\(path)/\(newName)"

            do {
                try self.fileManager.moveItem(atPath: oldPath, toPath: newPath)

                do {
                    try self.fileManager.removeItem(atPath: oldPath)
                    ok = true
                } catch _ {
                }
            } catch _ {
            }
            
        }
        return ok
    }
    
    
    public func documentsPathAtFolder(_ path:String) ->[AnyObject]
    {
        let directoryEmun = self.fileManager.enumerator(atPath: path)
        let contains = try? self.fileManager.contentsOfDirectory(atPath: path)
        
        for item in contains!
        {
            let type = item.self
            print(type)
//            println()
        }
        
        let paths = directoryEmun?.allObjects
//        print(paths)
        return paths! as [AnyObject]
    }
    
    
    
    
    public func replaceFileContentWith(_ regTag:String, replaceTag:String,filePath:String)
    {
        let fileData = self.fileManager.contents(atPath: filePath)
        print(filePath)

        if fileData != nil
        {
            let str = NSString(data: fileData!, encoding: String.Encoding.utf8.rawValue)
            
        
            let newString = str?.replace(byReg: regTag, tag: replaceTag)
            
            do {
                try newString?.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
            } catch _ {
            }
        }

    }
    

}
